package com.leather.quan.controller;

import com.leather.common.result.Result;
import com.leather.common.toolkits.ImageKit;
import com.leather.quan.service.QuanService;
import com.leather.quan.vo.QuanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.ArrayList;
import java.util.List;

@RestController
public class QuanController {

    @Autowired
    private QuanService quanService;


    @PostMapping("/addquan")
    public Result addQuan(MultipartFile image, QuanVo quanVo) {
        String imgUrl = "";
        if (image != null) {
            imgUrl = ImageKit.uploadImage(image, "quan");
        }
        quanVo.setImgUrl(imgUrl);
        return quanService.addQuan(quanVo);
    }


    @PostMapping("/addimg")
    public Result addQuanWithImg(MultipartRequest request, QuanVo quanVo) {
        List<MultipartFile> files = new ArrayList<>();
        files.add(request.getFile("img0"));
        files.add(request.getFile("img1"));
        files.add(request.getFile("img2"));
        List<MultipartFile> realFiles = new ArrayList<>();
        for (MultipartFile file:files) {
            if(file!=null) {
                realFiles.add(file);
            }
        }
        return quanService.addQuanWithMutiImage(quanVo,realFiles);
    }

    @GetMapping("/index")
    public Result index(int pageId){
        return quanService.getIndexData(pageId*20);
    }

    @PostMapping("/detail")
    public Result detail(int quanId){
        return quanService.getQuanDetail(quanId);
    }

    @PostMapping("/star")
    public Result star(int quanId){
        return quanService.touchStar(quanId);
    }

    @PostMapping("/delete")
    public Result delete(int quanId,int userId){
        return quanService.deleteQuan(quanId,userId);
    }

    @GetMapping("/userindex")
    public Result userIndex(int pageId,int userId) {
        return quanService.getQuanByUserId(pageId*20,userId);
    }

    @GetMapping("searchvalue")
    public Result searchValue(String searchValue) {
        return quanService.searchQuan(searchValue);
    }

}
